package entity.map;

import java.util.ArrayList;
import java.util.List;

import entity.GameEntity;
import entity.Position;
import astar.IAstarMap;

public class FieldEntityMap implements IFieldMapEntity, IAstarMap
{
	protected FieldEntity[][] _map;

	protected List<GameEntity> _listGameObject = null;

	public FieldEntityMap(int pSizeX, int pSizeY)
	{
		_map = new FieldEntity[pSizeX][pSizeY];
		_listGameObject = new ArrayList<GameEntity>();
	}

	public boolean isBlocked(Position pPosition)
	{
		return isPositionValid(pPosition)
				&& _map[pPosition.getX()][pPosition.getY()].isBlock();
	}

	public void setFieldEntity(FieldEntity pFieldEntity)
	{
		if (isPositionValid(pFieldEntity.getPosition()))
		{
			_map[pFieldEntity.getPosition().getX()][pFieldEntity.getPosition()
					.getY()] = pFieldEntity;
		}
	}

	private boolean isPositionValid(Position pPosition)
	{
		return pPosition.getX() > 0 && pPosition.getY() > 0
				&& pPosition.getX() < _map.length
				&& pPosition.getY() < _map[0].length;
	}

	@Override
	public FieldEntity getFieldEntity(Position pPosition)
	{
		FieldEntity result = null;
		if (isPositionValid(pPosition))
		{
			result = _map[pPosition.getX()][pPosition.getY()];
		}
		return result;
	}

	@Override
	public double getValue(Position pPos)
	{
		return 0;
	}

	@Override
	public void changeMainPosition(Position pPosition)
	{
		// TODO Auto-generated method stub

	}

	public int getSize()
	{
		return _map.length;
	}

	public void addGameEntity(GameEntity pGameEntity)
	{
		_listGameObject.add(pGameEntity);
	}

	public List<GameEntity> getGameEntitys()
	{
		return _listGameObject;
	}
}
